<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
  import Emitter from '../../utils/emitter'
  import { findComponentsDownward } from '../../utils/assist'
  export default {
    name: 'iRadioGroup',
    mixins: [Emitter],
    components: {},
    props: {
      value: {
        type: [String, Number, Boolean]
      }
    },
    data () {
      return {
        currentValue: this.value,
        children: []
      }
    },
    watch: {
      value() {
        this.updateModel(true)
      }
    },
    methods: {
      updateModel(update) {
        this.children = findComponentsDownward(this, 'iRadio')
        if (this.children.length) {
          const { value } = this
          // 做组件的更新处理
          this.children.forEach(child => {
            child.model = value
            if (update) {
              child.currentValue = value === child.label
              child.group = true
            }
          })
        }
      },
      change(data) {
        this.currentValue = data
        this.$emit('input', data)
        this.$emit('on-change', data)
        this.dispatch('iFormItem', 'on-form-change', data)
      }
    }
  }
</script>
<style lang='scss' scoped>
</style>
